<template>
  <div class="app-container">
    <el-tabs type="border-card">
      <el-tab-pane>
        <span slot="label">
          <i class="el-icon-date"></i> 文件存储配置
        </span>
        <el-form v-loading="Loading" style="margin-left: 20px;" :model="form" label-position="left" label-width="120px" :rules="rules" ref="form">
          <el-form-item label="本地图片域名" prop="localPictureBaseUrl">
            <el-input v-model="form.localPictureBaseUrl" style="width: 400px"></el-input>
          </el-form-item>

          <el-form-item label="七牛云图片域名" prop="qiNiuPictureBaseUrl">
            <el-input v-model="form.qiNiuPictureBaseUrl" auto-complete="new-password" style="width: 400px"></el-input>
          </el-form-item>

          <el-form-item label="七牛云公钥">
            <el-input v-model="form.qiNiuAccessKey" auto-complete="new-password" style="width: 400px"></el-input>
          </el-form-item>

          <el-form-item label="七牛云私钥">
            <el-input type="password" v-model="form.qiNiuSecretKey" auto-complete="new-password" style="width: 400px"></el-input>
          </el-form-item>

          <el-form-item label="上传空间">
            <el-input v-model="form.qiNiuBucket" style="width: 400px"></el-input>
          </el-form-item>

          <el-form-item label="存储区域">
            <el-select v-model="form.qiNiuArea" placeholder="请选择存储区域" clearable>
              <el-option v-for="item in sysStorageRegion.sysDictDataList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="图片上传七牛云">
            <el-radio-group v-model="form.uploadQiNiu" size="medium">
              <el-radio-button label="1" border>是</el-radio-button>
              <el-radio-button label="0" border>否</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="图片上传本地">
            <el-radio-group v-model="form.uploadLocal" size="medium">
              <el-radio-button label="1" border>是</el-radio-button>
              <el-radio-button label="0" border>否</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="图片显示优先级">
            <el-radio-group v-model="form.picturePriority" size="medium">
              <el-radio-button v-for="item in sysPicturePriority.sysDictDataList" :key="item.uid" :label="item.dictValue" border>{{item.dictLabel}}</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm()">保 存</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <el-tab-pane>
        <span slot="label">
          <i class="el-icon-date"></i> Redis缓存
        </span>
        <el-form style="margin-left: 20px;" label-position="left" label-width="120px">
          <aside>
            Redis管理主要用于清空一些缓存数据<br />
            用户首次部署时，可以使用清空全部，把Redis中的缓存一键清空<br />
          </aside>
          <el-form-item label="全部">
            <el-row>
              <el-col :span="6">
                <el-button type="danger" @click="cleanRedis('ALL')">清空全部</el-button>
              </el-col>
            </el-row>
          </el-form-item>

          <el-form-item label="博客相关">
            <el-row>
              <el-col :span="3">
                <el-button type="primary" @click="cleanRedis('BLOG_CLICK')">清空点击量</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="success" @click="cleanRedis('BLOG_PRAISE')">清空点赞量</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="info" @click="cleanRedis('BLOG_LEVEL')">清空推荐博客</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="warning" @click="cleanRedis('HOT_BLOG')">清空热门博客</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="danger" @click="cleanRedis('NEW_BLOG')">清空最新博客</el-button>
              </el-col>
            </el-row>
          </el-form-item>

          <el-form-item label="分类和归档相关">
            <el-row>
              <el-col :span="3">
                <el-button type="primary" @click="cleanRedis('MONTH_SET')">清空分类日期</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="success" @click="cleanRedis('BLOG_SORT_BY_MONTH')">清空分类数据</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="info" @click="cleanRedis('BLOG_SORT_CLICK')">清空分类点击量</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="warning" @click="cleanRedis('TAG_CLICK')">清空标签点击量</el-button>
              </el-col>
            </el-row>
          </el-form-item>

          <el-form-item label="系统相关">
            <el-row>
              <el-col :span="3">
                <el-button type="primary" @click="cleanRedis('REDIS_DICT_TYPE')">清空字典</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="success" @click="cleanRedis('ADMIN_VISIT_MENU')">清空角色访问菜单</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="info" @click="cleanRedis('userToken')">清空用户Token</el-button>
              </el-col>

              <el-col :span="3">
                <el-button type="warning" @click="cleanRedis('REQUEST_LIMIT')">清空接口请求限制</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <el-tab-pane>
        <span slot="label">
          <i class="el-icon-date"></i> ElasticSearch
        </span>
        <el-form style="margin-left: 20px;" label-position="left" label-width="120px">
          <aside>
            ElasticSearch用于存放文章索引<br />
            当对文章操作后es没有及时更新索引，可手动初始化es索引<br />
          </aside>
          <el-form-item label="搜索相关">
            <el-row>
              <el-col :span="6">
                <el-button type="danger" @click="initES()">初始化es</el-button>
              </el-col>
            </el-row>
          </el-form-item>

        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import { getSystemConfig, editSystemConfig, cleanRedisByKey, initES } from "@/api/systemConfig";
import { getListByDictTypeList } from "@/api/sysDictData";
export default {
  data() {
    return {
      form: {},
      sysPicturePriority: {}, // 存储区域字典
      sysStorageRegion: {}, //图片显示优先级字典
      rules: {
        localPictureBaseUrl: [
          { pattern: /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/, message: '请输入正确的域名' },
        ],
        qiNiuPictureBaseUrl: [
          { pattern: /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/, message: '请输入正确的域名' },
        ],
        email: [
          { pattern: /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/, message: '请输入正确的邮箱' },
        ],
      }
    };
  },
  created() {
    // 获取字典
    this.getDictList()
    // 获取系统配置
    this.getSystemConfigList();
  },
  methods: {
    getDictList() {
      var dictTypeList = [
        "sys_picture_priority",
        "sys_storage_region",
      ];
      this.Loading = true;
      getListByDictTypeList(dictTypeList).then(response => {
        this.sysPicturePriority = response.data.sys_picture_priority;
        this.sysStorageRegion = response.data.sys_storage_region;
        this.Loading = false;
      });
    },
    getSystemConfigList() {
      getSystemConfig().then(response => {
        if (response.data) {
          this.form = response.data;
        }
      });
    },
    submitForm() {
      this.$store.dispatch('app/setLoading', true);
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$store.dispatch('app/setLoading', true);
          editSystemConfig(this.form).then(res => {
            this.$notify({
              title: "成功",
              message: res.data,
              type: "success",
            });
          });
        }
      })
    },
    cleanRedis(key) {
      var params = new URLSearchParams();
      params.append("key", key);
      this.$store.dispatch('app/setLoading', true);
      cleanRedisByKey(params).then(res => {
        this.$notify({
          title: "成功",
          message: res.data,
          type: "success",
        });
      })
    },
    initES() {
      this.$store.dispatch('app/setLoading', true);
      initES().then(res => {
        this.$notify({
          title: "成功",
          message: res.data,
          type: "success",
        });
      })
    }
  }
}
</script>


<style lang="scss" scoped>
aside {
  background: #eef1f6;
  padding: 8px 24px;
  margin-bottom: 20px;
  border-radius: 2px;
  display: block;
  line-height: 32px;
  font-size: 16px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
  color: #2c3e50;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>